package com.apps.controller;

import com.alibaba.fastjson.JSON;
import com.apps.goods.model.po.KupaGoods;
import com.apps.goods.model.po.KupaGoodsCat;
import com.apps.goods.model.vo.KupaGoodsSearchVo;
import com.apps.goods.service.IKupaGoodsCatService;
import com.apps.goods.service.IKupaGoodsCommentService;
import com.apps.goods.service.IKupaGoodsService;
import com.apps.helper.UserConextHelper;
import com.apps.member.service.IKupaMemberService;
import com.apps.sannotation.constraints.StrDef;
import com.apps.saspect.ValidMethod;
import com.apps.sexctption.NullException;
import com.apps.vip.model.po.KupaVipGoods;
import com.apps.vip.service.IKupaVipGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Slf4j
@Controller
@RequestMapping("/page/goods")
public class GoodsController
{

    @Autowired
    private IKupaGoodsService goodsService;

    @Autowired
    private IKupaGoodsCatService goodsCatService;

    @Autowired
    private IKupaGoodsCommentService goodsCommentService;

    @Autowired
    private IKupaVipGoodsService vipGoodsService;

    @Autowired
    private IKupaMemberService memberService;

    /**
     * 商品分类首页
     * @param goods_cat_id 素材主分类的id
     * @param pageNo
     * @param keyworld 查询关键词
     * @param condition 排序条件
     * @param modelAndView
     * @param request
     * @return
     */
    @RequestMapping("/{goods_cat_id}/{pageNo}/index.html")
    public ModelAndView Index(@PathVariable Integer goods_cat_id,
                              @PathVariable Integer pageNo,
                              String  keyworld,
                              @RequestParam(value = "condition",required = false) List<Integer>  condition,
                              ModelAndView modelAndView,
                              HttpServletRequest request)
    {
        if(null == keyworld)
            keyworld="";
        modelAndView.setViewName("home/ppt_index");
        modelAndView.addObject("goods_cat_id",goods_cat_id);
        //商品查询条件
        List<KupaGoodsSearchVo> searchVos=goodsService.queryGoodsSearchParam(goods_cat_id);
        modelAndView.addObject("goodssearch",searchVos);
        //塞入查询关键词
        modelAndView.addObject("keyworld",keyworld);
        //初始化附加调件词数组
        if(null == condition)
            condition = new ArrayList<>(Collections.nCopies(searchVos.size(), 0));
        //分页查询出商品数据
        modelAndView.addObject("goodslist",goodsService.searchGoods(keyworld,condition,goods_cat_id,pageNo,10));
        //塞入查询附加词
        modelAndView.addObject("condition",condition.toString());
        return modelAndView;
    }

    /**
     * 商品详情页
     * @param modelAndView
     * @param request
     * @return
     */
    @RequestMapping("/{goods_id}/ppt-details.html")
    public ModelAndView ppt_details(@PathVariable Integer goods_id,
                                    ModelAndView modelAndView,
                                    HttpServletRequest request)
    {

        modelAndView.setViewName("home/ppt_details");
        KupaGoods goods=goodsService.getOneById(goods_id);
        if(null == goods)
            throw new NullException("商品"+goods_id+"不存在");
        if(null != UserConextHelper.getCurrentMember())
           memberService.viewsGoods(UserConextHelper.getCurrentMember().getId(),goods);
        //填入数据
        modelAndView.addObject("goods",goods);
        //相似推荐
        modelAndView.addObject("resembleRecommend",goodsService.resembleRecommend(goods,8));
        //商品评论数据
        modelAndView.addObject("goodsComment",goodsCommentService.queryByGoodsId(goods_id));
        //商品分类数据
        modelAndView.addObject("goodscatnames", JSON.parseArray(goodsCatService.getOneById(goods.getGoods_cat_id()).getFather_name_tree(),String.class));
        return modelAndView;
    }
    /**
     * 购买页面
     * @param modelAndView
     * @param request
     * @return
     */
    @RequestMapping("/{goods_id}/buy.html")
    public ModelAndView Buy(@PathVariable Integer goods_id,
                            ModelAndView modelAndView,
                            HttpServletRequest request)
    {
        modelAndView.setViewName("home/buy");
        KupaGoods goods=goodsService.getOneById(goods_id);
        modelAndView.addObject("goods",goods);
        //商品分类数据
        modelAndView.addObject("goodscatnames", JSON.parseArray(goodsCatService.getOneById(goods.getGoods_cat_id()).getFather_name_tree(),String.class));
        //剩余免费下载次数
        if(null != UserConextHelper.getCurrentMember())
           modelAndView.addObject("freecount",memberService.getTotalDownloadFreeCountByMemberId(UserConextHelper.getCurrentMember().getId()));
        return modelAndView;
    }
}
